﻿
//返回目标年月
function getSelDate(year,month)
{
	var sel_date = new Date();
	sel_date.setFullYear(year,month,1);	
	return sel_date;
}

//根据年份和月份判断这个月有多少天getDayNum(y,m)
function getDayNum(y,m)
{
	var il = new Array(31,29,31,30,31,30,31,31,30,31,30,31);//闰年月份天数表
	var nl = new Array(31,28,31,30,31,30,31,31,30,31,30,31);//平年月份天数表
	
	//判断是否是闰年
	var l = ((y%4)==0 && y%100 !=0)||(y%400==0)?true:false;
	return l==true ? il[m-1]: nl[m-1];
}


//判断该年该月的1号是星期几,按照中文日历习惯，就是星期一等于1，星期日等于7
function whatweek(y,m)
{
	var date = new Date();
	date.setFullYear(y,m,1);	
	t=date.getDay();	
	return (t==0)?7:t+1;
}

/*
获取月份数据，默认是类似如下格式：
0123456
78910111213
14151617181920
21222324252627
28000000
0000000
*/
function getMonthData(y,m)
{
	
	var cal = new Array();//日历数据数组
	
	var sd=1;//日历默认起始位置
	var ed=getDayNum(y,m);//得到这个月有多少天
	var ww = whatweek(y,m);//得到这个月1号是星期几
	
	for(i=0;i<6;i++)
	{		
		cal[i] = new Array();
		for(j=0;j<7;j++)
		{
			
			if(((i == 0) && (j+1)< ww) || (sd>ed))
			{
				cal[i][j]=0;				
			}
			else
			{
				cal[i][j]=sd++;			
			}			
		}
	}
	return cal;
}

//返回一个html输出用的日历
function createCalBody(y,m)
{
	var res = "";
	//获取对应日历数据
	var x = getMonthData( y,m);
	
	res += "<table>";
	
	res+=  "<tr><td><span style='font-weight: bold; font-size:22px;'>一</span></td><td><span style='font-weight: bold; font-size:22px;'>二</span></td><td><span style='font-weight: bold; font-size:22px;'>三</span></td><td><span style='font-weight: bold; font-size:22px;'>四</span></td><td><span style='font-weight: bold; font-size:22px;'>五</span></td><td style='color:red;'><span style='font-weight: bold; font-size:22px;'>六</span></td><td style='color:red;'><span style='font-weight: bold; font-size:22px;'>日</span></td></tr>"
	
	line=x[5][0]==0?5:6;//判断这个日历是应该有5行还是6行
	
	for(i=0;i<line;i++)
	{		
		res += "<tr>";
		for(j=0;j<7;j++)
		{			
			var t = x[i][j];
			
			res += "<td class='cal-item cal-item-"+t+"'>";			
			
			res += t==0? "": t;	
			
			res += "</td>";
		}
		res += "</tr>";
	}
	res += "</table>";
	return res;
}

//创建日历头部控制部分
function createCalHead()
{
	var res="";
	var date=new Date();
	
	var sel_year="<select name='sel_year' id='sel_year'>"+ createSelOption(1901,2099,date.getFullYear(),"年")+"</select>";
	var sel_mon="<select name='sel_mon' id='sel_mon'>"+ createSelOption(1,12,date.getMonth()+1,"月")+"</select>";
	
	
	res += sel_year;
	res += sel_mon;
	return res;
	
}
//为下拉菜单创建项

function createSelOption(start,end,val,other)
{
	var res="";
	for(i=start;i<=end;i++)
	{
		if(i==val)
		{
			res+="<option value='"+ i + "' selected>" +i + other +"</option>";
		}
		else
		{
			res+="<option value='"+ i + "'>" +i + other +"</option>";
		}
	}
	return res;
}

//属性日历改变
function FlushCal()
{


    var y=$("#sel_year").val();
    var m=$("#sel_mon").val();
	var cal_body = createCalBody(y,m);
	$(".cal-body").html(cal_body);	
	
	//渲染
	Render() ;
}

//创建完整日历到 el这个jquery元素上
function createFullCal(el)
{
	el.html("<div>"+
				"<div class='cal-head'></div>"+
				"<div class='cal-body'></div>"+
			"</div>");
	
	var cal_head = createCalHead();	
	$(".cal-head").html(cal_head);
	
    var y=$("#sel_year").val();
    var m=$("#sel_mon").val();
	var cal_body = createCalBody(y,m);
	
	$(".cal-body").html(cal_body);	
	
	
	//为日历绑定事件
	$("#sel_year,#sel_mon").change(FlushCal);	
}